<?php

namespace App\Hospital\Controllers;

use App\Hospital\Repositories\Article;
use App\Models\FamilyType;
use App\Models\Hospital;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleController extends AdminController
{
    protected function description()
    {
        $this->description['index'] = '社区自行添加的文章会在推送宣教指导时，根据是否开启推送推送给针对人群';
        $this->description['create'] = '社区自行添加的文章会在推送宣教指导时，根据是否开启推送推送给针对人群';

        return $this->description;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Article(['familyType','info']), function (Grid $grid) {
            $grid->model()->where('type',1)->wherein('hospital_id',[0,Admin::user()->hospital_id])->orderby('id','desc');
            $grid->column('info.title','文章标题');
            $grid->familyType('针推人群')->pluck('name')->label();
            $grid->column('is_push','是否推送')
            ->if(function ($column) {

                return $this->hospital_id == Admin::user()->hospital_id ;
            })
            ->switch('', true)->else()
            ->display('是');;

            $grid->disableBatchDelete();//禁用批量操作
            $grid->disableRowSelector();//禁用行选择器
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableActions();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if($this->hospital_id != Admin::user()->hospital_id){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableQuickEdit();
                }
            });
            $grid->filter(function($filter){
                // 禁止自动展开过滤器
                $filter->expand(false);
                $filter->panel();

                $filter->like('info.title', '文章标题');
                $filter->like('info.content', '文章内容');
                //$filter->like('type', '文章内容');
                //$filter->in('id')->multipleSelect(); // 设置编辑数据显示


            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {

            $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });
            $show->field('id');
            $show->field('status');
            $show->field('type');
            $show->field('subject_pid');
            $show->field('subject');
            $show->field('hospital_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $hospital = Hospital::query()->where('id',Admin::user()->hospital_id)->first();
        return Form::make(Article::with(['info','FamilyType']), function (Form $form) use($hospital) {
            $form->text('info.title','文章标题')->required();
            $form->text('info.ftitle','文章副标题')->required();
            $form->image('info.img','文章封面')
                ->uniqueName()
                ->autoUpload()
                ->accept('jpg,png,gif,jpeg', 'image/*')
                ->maxSize(500)->required();
            $form->textarea('info.content','文章内容')->required();
            $form->hidden('info.source')->value($hospital->name);
            $form->multipleSelect('familyType','文章对应人群')
                ->options(FamilyType::query()->pluck('name','id'))
                ->customFormat(function ($v) {
                    if (! $v) {
                        return [];
                    }

                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($v, 'id');
                })->required();
            $form->hidden('type')->value(1)->required();
            $form->hidden('hospital_id')->value(Admin::user()->hospital_id)->required();
            $form->switch('is_push')->required()
            ->saving(function ($v) {
                return $v ? 1 : 0;
            });
        });
    }
}
